package com.hzw.saas.common.flow.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.annotation.Resource;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzw.saas.common.flow.mapper.FlowProcessMapper;
import com.hzw.saas.common.flow.pojo.data.UserInfo;
import com.hzw.saas.common.flow.pojo.model.FlowInstance;
import com.hzw.saas.common.flow.pojo.model.FlowProcess;
import com.hzw.saas.common.flow.service.IFlowInstanceService;
import com.hzw.saas.common.flow.service.IFlowProcessPermissionService;
import com.hzw.saas.common.flow.service.IFlowProcessService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author sonam
 * @since 2021-08-04
 */
@Service
public class FlowProcessServiceImpl extends ServiceImpl<FlowProcessMapper, FlowProcess> implements IFlowProcessService {

    @Resource
    private IFlowInstanceService flowInstanceService;
    @Resource
    private IFlowProcessPermissionService flowProcessPermissionService;


    @Override
    public FlowProcess getNextFlowProcess(FlowInstance flowInstance, String flowProcessId, UserInfo userInfo) {
        if (Objects.isNull(flowInstance))
            return null;
        FlowProcess flowProcess = super.lambdaQuery()
            .eq(FlowProcess::getPid, flowProcessId)
            .eq(FlowProcess::getFlowNodePreId, flowInstance.getFlowNodeId())
            .eq(FlowProcess::getFlowId, flowInstance.getFlowId())
            .one();
        if (Objects.isNull(flowProcess))
            return null;
        boolean check = flowProcessPermissionService.checkFlowProcess(flowProcessId, flowInstance.getSubmitUserId(), userInfo);
        return check ? flowProcess : null;
    }

    @Override
    public List<FlowProcess> listNextFlowProcesses(String flowInstanceId, UserInfo userInfo) {
        FlowInstance flowInstance = flowInstanceService.getById(flowInstanceId);
        return this.listNextFlowProcesses(flowInstance, userInfo);
    }

    @Override
    public List<FlowProcess> listNextFlowProcesses(String flowInstanceId, UserInfo userInfo, Integer visible) {
        FlowInstance flowInstance = flowInstanceService.getById(flowInstanceId);
        return this.listNextFlowProcesses(flowInstance, userInfo, visible);
    }

    @Override
    public List<FlowProcess> listNextFlowProcesses(FlowInstance flowInstance, UserInfo userInfo) {
        return this.listNextFlowProcesses(flowInstance, userInfo, null);
    }

    @Override
    public List<FlowProcess> listNextFlowProcesses(FlowInstance flowInstance, UserInfo userInfo, Integer visible) {
        if (Objects.isNull(flowInstance))
            return new ArrayList<>();
        List<FlowProcess> flowProcesses = super.lambdaQuery()
            .eq(FlowProcess::getFlowId, flowInstance.getFlowId())
            .eq(FlowProcess::getFlowNodePreId, flowInstance.getFlowNodeId())
            .eq(visible != null, FlowProcess::getFlowProcessVisible, visible)
            .list();
        List<String> flowProcessIds = flowProcesses.stream().map(FlowProcess::getPid).collect(Collectors.toList());
        List<String> filteredFlowProcessIds = flowProcessPermissionService.filterFlowProcesses(flowProcessIds, flowInstance.getSubmitUserId(), userInfo);
        return flowProcesses.stream().filter(flowProcess -> filteredFlowProcessIds.contains(flowProcess.getPid())).collect(Collectors.toList());
    }

}
